﻿/*
   Copyright 2014 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using Flower.Services;
using Flower.Services.Data;
using Jurassic;
using Jurassic.Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flower.Directory.Scripting.Functions.Waiting
{
    internal class GetSuspended : ClrFunction
    {
        public GetSuspended(ScriptEngine engine)
            : base(engine.Function.InstancePrototype, "__getSuspended", engine.Function.InstancePrototype)
        {
        }

        [JSCallFunction]
        public object Exec(string processorName, int offset, int limit)
        {
            try
            {
                IDirectory dir = Engine.GetDirectory();

                var result = Exec(dir, processorName, offset, limit);

                var pids = Engine.Array.Construct();
                var timeouts = Engine.Array.Construct();
                var linkIds = Engine.Array.Construct();

                foreach (string pid in (IList<string>)result["pids"])
                {
                    pids.Push(pid);
                }

                foreach (string pid in (IList<string>)result["linkIds"])
                {
                    linkIds.Push(pid);
                }

                foreach (double timeout in (IList<double>)result["timeouts"])
                {
                    timeouts.Push(timeout);
                }

                return
                    Engine
                        .Object
                        .Construct()
                        .Set("linkIds", linkIds)
                        .Set("pids", pids)
                        .Set("timeouts", timeouts);
            }
            catch (JavaScriptException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new JavaScriptException(Engine, "Error", ex.Message, ex);
            }
        }

        [ClrScript]
        public static Dictionary<string, object> Exec(IDirectory dir, string processorName, int offset, int limit)
        {
            var links =
                dir.GetChildren
                (
                    string.Concat("/Processors/", processorName, "/Suspended"),
                    DirectoryEntryTypes.Link,
                    SortProperty.Default,
                    SortOrder.Asc,
                    offset,
                    limit
                );

            var pids = new List<string>();
            var timeouts = new List<double>();
            var linkIds = new List<string>();
            var engine = new ScriptEngine();

            foreach (Link l in links)
            {
                linkIds.Add(l.Id);
                pids.Add(l.Target);
                timeouts.Add(engine.Date.Construct(l.Name.Substring(0, l.Name.IndexOf("#"))).ValueInMilliseconds);
            }

            return new Dictionary<string, object>
            {
                { "linkIds", linkIds },
                { "pids", pids },
                { "timeouts", timeouts },
            };
        }
    }
}
